/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package xmarti.vacaxm.model;

import xmarti.vacaxm.utils.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import xmarti.vacaxm.beans.connect.User;



/**
 *
 * @author xmarti
 */
public class ModelPerson extends HibernateUtil {
    public List<User> getActiveUsers(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();				
		List<User> users=new ArrayList<User>();
		try{
			Query query=session.createQuery("from User u WHERE u.active=:p1 order by u.login ");			
			query.setParameter("p1", true);
                        users = (List<User>)query.list();
			session.getTransaction().commit();
		}catch (HibernateException he){
			System.err.println("Error Hibernate: " + he.getMessage());
			session.getTransaction().rollback();
		}		
		
		return users;		
	}
    public List<User> getUsers(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();				
		List<User> users=new ArrayList<User>();
		try{
                    Query query=session.createQuery("from User u order by u.login");			
                    users = (List<User>)query.list();
                    session.getTransaction().commit();
		}catch (HibernateException he){
                    System.err.println("Error Hibernate: " + he.getMessage());
                    session.getTransaction().rollback();
		}		
		
		return users;		
	}
    
    public User getUserByLogin(String login){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
            User user=new User();
            try{
                Query query=session.createQuery(" from User u where u.cif=:log");
                query.setParameter("log", login);
                user = (User)query.uniqueResult();                                                                         
            }catch (HibernateException he){
                System.err.println("Error Hibernate: " + he.getMessage());
                session.getTransaction().rollback();
            }				
            return user;
    }
    public  <T> List<T> getListPerson(Class<T> entityClass) {        
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            List<T> resultList = null;
            try
            {            
                resultList= session.createQuery("FROM " + entityClass.getSimpleName()+ " e  order by e.cif").list();
            }
            catch (HibernateException he)
            {
                System.out.println(he.getClass()+" " +he.getCause()+ " "+he.getMessage());
            }
            return resultList;
        }
    public  <T> List<T> getListPersonActive(Class<T> entityClass) {        
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            List<T> resultList = null;
            try
            {            
                resultList= session.createQuery("FROM " + entityClass.getSimpleName()+ " e where e.active=true order by e.cif").list();
            }
            catch (HibernateException he)
            {
                System.out.println(he.getClass()+" " +he.getCause()+ " "+he.getMessage());
            }
            return resultList;
        }
}
